#!/bin/env lua
if arg[0] == "get-all-js.lua" then
	dofile("config.lua")
end

OUTFILE_P = TEMP.."data-%s-%s.js"

local function execute(cmd, ...)
	if cmd:find("%", nil, true) then
		cmd = cmd:format(...)
	end
	if arg[1] == '-v' then print(cmd) end
	os.execute(cmd)
end

function get_content(filename)
	local status, f = pcall(io.open, filename, "r")
	if not status or not f then return nil end
	local content = f:read("*a")
	f:close()
	return content
end

function download(src, dst)
	execute("curl -s -o %s %s", dst, src)
end

local function iconv(file, from, to)
	execute("mv %s %s.old", file, file)
	execute("iconv -c -s -f %s -t %s %s.old -o %s", from, to, file, file)
	execute("rm %s.old", file)
end

local function get_start_bytes(filename, len)
	local f = io.open(filename, "r")
	local c = f:read(len)
	f:close()
	return c
end
local UTF8_MARKER = "\239\187\191"

function get_lang_data(lang, url_p, verbose)
	local changed = nil
	for class in pairs(CLASSES) do
		local url = string.format(url_p, class:lower(), lang)
		local out = string.format(OUTFILE_P, class:lower(), lang)
		local prev = get_content(out)
		download(url, out)
		local encoding = LANG_ENCODING[lang]
		if encoding and encoding ~= "UTF-8" then
			if get_start_bytes(out, 3) ~= UTF8_MARKER then
				iconv(out, encoding, "UTF-8")
			end
		end
		local new = get_content(out)
		if new ~= prev then
			if verbose then print("CHANGED", url) end
			changed = true
		end
	end
	return changed
end

function get_all_data(verbose, l)
	if l then
		return get_lang_data(l, LANG_SRC_URLS[l], verbose)
	else
		local changed = {}
		for lang, url_p in pairs(LANG_SRC_URLS) do
			changed[lang] = get_lang_data(lang, url_p, verbose)
		end
		return changed
	end
end

if arg[0] == "get-all-js.lua" then
	get_all_data(true, arg[1])
end
